package com.cztv.vote.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.cztv.vote.bean.VoteProjectBean;
import com.cztv.vote.bean.VoteRecord;

public class VoteDao extends SimpleJdbcDaoSupport {

	@SuppressWarnings("unchecked")
	public List queryAll(){
		String sql = "select * from project where p_open=1";
		
		List list=this.getJdbcTemplate().query(sql, new RowMapper() {

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				VoteProjectBean bean = new VoteProjectBean();
				bean.setPid(rs.getInt("p_id"));
				bean.setCode(rs.getInt("is_code") == 1 ? true : false);
				bean.setIp(rs.getInt("is_ip") == 1 ? true : false);
				bean
						.setIspOpen(rs.getInt("p_open") == 1 ? true
								: false);
				bean.setVoteNum(rs.getInt("is_webvote") == 1 ? true
						: false);

				int unit = rs.getInt("p_unit");
				int ipTimelimit = rs.getInt("is_iptime");

				bean.setIpTimeLimit(unit * ipTimelimit);
				bean.setVoteNumLimit(rs.getInt("is_votenum"));

				bean.setRegtime(rs.getLong("p_regtime"));
				bean.setEndtime(rs.getLong("p_offtime"));

				return bean;
			}
			
		});
		
		return list;
	}
	
	public int getCountById(int rid){
		String sql="select votes from votes where r_id=? and v_type=1";
		return this.getJdbcTemplate().queryForInt(sql, new Object[]{rid});
	}
	
	public void insert(VoteRecord record) {
		String sql = "insert into votelogs(r_id,v_ip,v_time) values(?,?,?)";
		this.getJdbcTemplate().update(
				sql,
				new Object[] { record.getVoteId(), record.getVoteIp(),
						record.getVoteTime() });
	}

	public void updateLogs() {
		String clearSql="update votelogs set v_sign=1 where v_sign=0";
		this.getJdbcTemplate().update(clearSql);
	}
	
	public void updateVotes(int votes,int rid){
		String updateSql="update votes set votes=? where r_id=? and v_type=1";
		this.getJdbcTemplate().update(updateSql,new Object[]{votes,rid});
	}
	
	public Map<Integer,Integer> countByIds(){
		String sql="select r_id,votes from votes where v_type=1";
		
		final Map<Integer,Integer> countMap=new HashMap<Integer,Integer>();
		
		this.getJdbcTemplate().query(sql,new RowMapper(){
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				countMap.put(rs.getInt("r_id"), rs.getInt("votes"));
				return null;
			}
			
		});
		
		return countMap;
	}
	
	public Map<Integer,Integer> countByGroup(){
		String sql="select r_id,count(*) num from votelogs where v_sign=0 group by r_id having num<>0";
		
		final Map<Integer,Integer> countMap=new HashMap<Integer,Integer>();
		
		this.getJdbcTemplate().query(sql,new RowMapper(){
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				countMap.put(rs.getInt("r_id"), rs.getInt("num"));
				return null;
			}
			
		});
		return countMap;
	}
	
	public int queryCountByIp(String ip) {
		String sql = "select count(*) from votelogs where v_ip=?";
		int count = this.getJdbcTemplate()
				.queryForInt(sql, new Object[] { ip });
		return count;
	}
	
	public long queryTimeByIp(String ip){
		String sql = "select max(v_time) from votelogs where v_ip=?";
		long startTime=this.getJdbcTemplate().queryForLong(sql,  new Object[] { ip });
		return startTime;
	}

	public Object queryById(int rid) {
		String sql = "select * from project where p_id=(select p_id from register where r_id=?)";

		Object obj = this.getJdbcTemplate().queryForObject(sql,
				new Object[] { rid }, new RowMapper() {

					public Object mapRow(ResultSet rs, int index)
							throws SQLException {
						VoteProjectBean bean = new VoteProjectBean();
						bean.setCode(rs.getInt("is_code") == 1 ? true : false);
						bean.setIp(rs.getInt("is_ip") == 1 ? true : false);
						bean
								.setIspOpen(rs.getInt("p_open") == 1 ? true
										: false);
						bean.setVoteNum(rs.getInt("is_webvote") == 1 ? true
								: false);

						int unit = rs.getInt("p_unit");
						int ipTimelimit = rs.getInt("is_iptime");

						bean.setIpTimeLimit(unit * ipTimelimit);
						bean.setVoteNumLimit(rs.getInt("is_votenum"));

						bean.setRegtime(rs.getLong("p_regtime"));
						bean.setEndtime(rs.getLong("p_offtime"));

						return bean;
					}
				});

		return obj;
	}
}
